package com.cxyd.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cxyd.common.Constant;
import com.cxyd.common.PageResult;
import com.cxyd.common.QueryPageBean;
import com.cxyd.common.RestResponse;
import com.cxyd.domain.School;
import com.cxyd.service.SchoolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Author: huanglianyun
 * @Description:
 * @DATE: Created in 2020/2/16 20:32.
 */
@RestController
@RequestMapping("/school")
@Log4j2
@Api(value = "校区信息管理接口", description = "提供校区信息增删改查")
public class SchoolRest {

    @Autowired
    SchoolService schoolService;

    @PostMapping(value = "/add")
    @ApiOperation("校区信息新增接口")
    @ApiImplicitParams({//多个请求参数
            @ApiImplicitParam(name="school",value = "校区信息实体类",dataType = "School")
    })
    public RestResponse add(@RequestBody School school){
        try{
            school.setCreateTime(new Date());
            schoolService.addSchool(school);
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.error(Constant.REST_RESP_error_MSG);
        }
        return RestResponse.ok(Constant.REST_RESP_OK_MSG);
    }

    //分页查询
    @ApiOperation("分页查询学生信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="queryPageBean",value = "分页查询bean",dataType = "QueryPageBean"),//一个请求参数
            @ApiImplicitParam(name = "schoolName", value = "校区姓名,模糊查询",  dataType = "String"),
            @ApiImplicitParam(name = "schoolHead", value = "校区负责人,模糊查询",  dataType = "String"),
            @ApiImplicitParam(name = "province", value = "所在省份,模糊查询",  dataType = "String")
    })
    @PostMapping(value = "/findPage")
    public RestResponse getPrjUser(@RequestBody QueryPageBean queryPageBean,
                                   @RequestParam(required = false)  String schoolName,
                                   @RequestParam(required = false)  String schoolHead,
                                   @RequestParam(required = false) String province) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        //String queryString = queryPageBean.getQueryString();//查询条件

        Long total = null;
        List<School> rows = null;
        PageResult pageResult = null;
        try{
            Page<School> page = new Page<>(currentPage == null? 1 : currentPage, pageSize == null ? 5 : pageSize);

            IPage<School> stuIPage = schoolService.selectPageSchool(page,schoolName,schoolHead,province);
            total = stuIPage.getTotal();
            rows = stuIPage.getRecords();

            pageResult = new PageResult(total, rows);
        }catch (Exception e){
            e.printStackTrace();
            return RestResponse.error(Constant.REST_RESP_error_MSG);
        }
        return RestResponse.ok(pageResult);
    }
}
